package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.adm.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.TrainingMaterialMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.TrainingMaterial;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.IamFileService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.TrainingMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 培训材料服务实现
 * Created by yangsy on 2016/11/24.
 */

public class TrainingMaterialServiceImpl implements TrainingMaterialService {

    @Autowired
    private TrainingMaterialMapper trainingMaterialMapper;
    @Autowired
    private IamFileService iamFileService;

    /**
     * 删除培训材料
     * @param trainingMaterialIds 要删除的id数组
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String[] trainingMaterialIds){
        if (trainingMaterialIds.length>0){
            return  trainingMaterialMapper.deleteByPrimaryKey(trainingMaterialIds);
        }
        return 0;
    }
    /**
     * 新增培训材料
     * @param record
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(TrainingMaterial record,String uploadFiles,String deletedFiles) {
        // 数据保存成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return 0;
        }
        resultCount +=  trainingMaterialMapper.insertSelective(record);
        // 保存附件
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getTrainingMaterialId());

        return  resultCount;
    }
    /**
     * 根据id查询
     * @param trainingMaterialId 培训材料id
     * @return
     */
    @Override
    public TrainingMaterial selectByPrimaryKey(String trainingMaterialId){
        if (!"".equals(trainingMaterialId)){
            // 根据id查询培训材料信息
            TrainingMaterial trainingMaterial = trainingMaterialMapper.selectByPrimaryKey(trainingMaterialId);
            // 如果使用岗位类型为空则直接返回
            if(trainingMaterial.getJobTypeIdSuite()==null){
                return trainingMaterial;
            }
            return trainingMaterial;
        }
        return null;
    }

    /**
     * 更新培训材料
     * @param record
     * @param uploadFiles 需要上传的文件
     * @param deletedFiles 需要删除的文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(TrainingMaterial record,String uploadFiles,String deletedFiles) {
        // 数据更新成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return 0;
        }
        resultCount +=  trainingMaterialMapper.updateByPrimaryKeySelective(record);
        if(null != record.getTrainingMaterialId()){
            //保存附件
            resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getTrainingMaterialId());
        }

        return  resultCount;
    }

    /**
     * 查询列表
     * @param bootstrapSearchBean
     * @return
     */
    @Override
    public JSONObject list(BootstrapSearchBean bootstrapSearchBean) {
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        bootstrapSearchBean.setLimit( bootstrapSearchBean.getLimit() == null ? Integer.valueOf(10) : bootstrapSearchBean.getLimit() );
        bootstrapSearchBean.setOffset( bootstrapSearchBean.getOffset() == null ? Integer.valueOf(0) : bootstrapSearchBean.getOffset() );
        // 总条数
        jsonObject.put("total",trainingMaterialMapper.queryCount(bootstrapSearchBean));
        List<TrainingMaterial> trainList = trainingMaterialMapper.queryList(bootstrapSearchBean);
        jsonObject.put("rows", JSON.toJSON(trainList));
        return jsonObject;
    }

    /**
     * 根据培训材料名称收索
     * @param name 材料名
     * @return
     */
    @Override
    public  List<TrainingMaterial> queryListByName(String name){
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        List<TrainingMaterial> materialList=trainingMaterialMapper.queryListByName(map);
        return  materialList;
    }

    /**
     * 判断培训材料名是否存在
     * @param materialName 培训材料名
     * @return true:存在 false:不存在
     */
    @Override
    public boolean isExitByName(String materialName) {
        return trainingMaterialMapper.isExitByName(materialName) > 0 ? true : false;
    }

}
